// pages/user/commission-sum/index.js
const app = getApp()
const api  = require('../../../api/user')
import {initLoadMoreMixin} from '../../../utils/list-load-more'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabItems: [{
      title: '全部',
      type:''
    }, {
      title: '今日',
      type: 0
    }, {
      title: '本周',
      type: 1
    }, {
      title: '本月',
      type: 2
    }],
    activeIndex: 0,
    shopId: app.getUser().id,
    totalCommission: 0,
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    initLoadMoreMixin.call(this)
    this.resetInit()
  },
  initData() {
    const type = this.data.tabItems[this.data.activeIndex].type;
    return app.requestUrl({
      url:api.COMMISSION_SUM, 
      data:{
        shopId: this.data.shopId,
        type: type,
        offset: (this.data.pages.currPage - 1) * this.data.pages.perPage,
        limit: this.data.pages.perPage
      }
    }).then(res => {
      const {
        commissionTotal,  
        logs,
      } = res.data.data;
      var rows = logs
                 ? logs.rows
                 : []
      this.setData({totalCommission: commissionTotal,items: this.data.items.concat(rows)})
      return {
        pageCount: rows.length,
        totalCount: logs && logs.total
      };
    });
  },
  handleTabClick(e) {

      //this.activeIndex = index;
      this.setData({activeIndex: e.detail.index})
      this.resetInit();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})